package org.seckill.service.impl;

import org.seckill.entity.Seckill;
import org.seckill.entity.SuccessKilled;
import org.seckill.service.BusinessService;
import org.seckill.service.SeckillService;
import org.seckill.service.SuccessKilledService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author: zhangyingxuan
 * @Date: 2018/9/19 下午3:33
 */
@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private SeckillService seckillService;

    @Autowired
    private SuccessKilledService successKilledService;

    @Autowired
    private ThreadPoolTaskExecutor executorService;

//    @Transactional
//    @Override
//    public void testTransaction() throws Throwable {
//        Seckill seckill = new Seckill();
//        seckill.setEndTime(new Date());
//        seckill.setStartTime(new Date());
//        seckill.setName("ceshiceshi");
//        seckill.setNumber(100);
//        seckillService.insert(seckill);
//        SuccessKilled successKilled = new SuccessKilled();
//        successKilled.setSeckillId(seckill.getSeckillId());
//        successKilled.setUserPhone(12345678911L);
//
//        successKilledService.insert(successKilled);
//        //抛出异常事务回滚
//        throw new RuntimeException("ceshi rollback");
//    }

//    @Transactional
//    @Override
//    public void testTransaction() throws Throwable {
//        Seckill seckill = new Seckill();
//        seckill.setEndTime(new Date());
//        seckill.setStartTime(new Date());
//        seckill.setName("ceshiceshi");
//        seckill.setNumber(100);
//        seckillService.insert(seckill);
//        SuccessKilled successKilled = new SuccessKilled();
//        successKilled.setSeckillId(seckill.getSeckillId());
//        successKilled.setUserPhone(12345678911L);
//
//        //异常情况1，事务不回滚
//        Thread thread = new Thread(() -> {
//            successKilledService.insert(successKilled);
//            throw new RuntimeException("ceshi rollback");
//        });
//        List<Throwable> throwableList = new ArrayList<>();
//        thread.setUncaughtExceptionHandler((t, e) -> {
//            throwableList.add(e);
//        });
//
//
//        thread.start();
//        if (!throwableList.isEmpty()) {
//            throw throwableList.get(0);
//        }
//    }

//    @Transactional
//    @Override
//    public void testTransaction() throws Throwable {
//        Seckill seckill = new Seckill();
//        seckill.setEndTime(new Date());
//        seckill.setStartTime(new Date());
//        seckill.setName("ceshiceshi");
//        seckill.setNumber(100);
//        seckillService.insert(seckill);
//        SuccessKilled successKilled = new SuccessKilled();
//        successKilled.setSeckillId(seckill.getSeckillId());
//        successKilled.setUserPhone(12345678911L);
//
//        //异常情况2，外层的可以回滚，内层不会回滚
//        Thread thread = new Thread(() -> {
//            successKilledService.insert(successKilled);
//            throw new RuntimeException("ceshi rollback");
//        });
//        List<Throwable> throwableList = new ArrayList<>();
//        thread.setUncaughtExceptionHandler((t, e) -> {
//            throwableList.add(e);
//        });
//
//
//        thread.start();
//        thread.join();
//        if (!throwableList.isEmpty()) {
//            throw throwableList.get(0);
//        }
//    }

//    @Transactional
//    @Override
//    public void testTransaction() throws Throwable {
//        Seckill seckill = new Seckill();
//        seckill.setEndTime(new Date());
//        seckill.setStartTime(new Date());
//        seckill.setName("ceshiceshi");
//        seckill.setNumber(100);
//        seckillService.insert(seckill);
//        SuccessKilled successKilled = new SuccessKilled();
//        successKilled.setSeckillId(seckill.getSeckillId());
//        successKilled.setUserPhone(12345678911L);
//
//        //外层的可以回滚，内层不会回滚
//        Thread thread = new Thread(() -> {
//            //insert发生异常， 这里会在父线程拿到异常信息并抛出， 但是处理要注意一定要子线程内部处理掉回滚的全部事情
//
//            successKilledService.insert(successKilled);
//            //throw new RuntimeException("ceshi rollback");
//        });
//        List<Throwable> throwableList = new ArrayList<>();
//        thread.setUncaughtExceptionHandler((t, e) -> {
//            throwableList.add(e);
//        });
//
//
//        thread.start();
//        thread.join();
//        if (!throwableList.isEmpty()) {
//            throw throwableList.get(0);
//        }
//    }

    @Transactional
    @Override
    public void testTransaction() throws Throwable {
        Seckill seckill = new Seckill();
        seckill.setEndTime(new Date());
        seckill.setStartTime(new Date());
        seckill.setName("ceshiceshi");
        seckill.setNumber(100);
        seckillService.insert(seckill);
        SuccessKilled successKilled = new SuccessKilled();
        successKilled.setSeckillId(seckill.getSeckillId());
        successKilled.setUserPhone(12345678911L);

        executorService.execute(() -> {
            successKilledService.insert(successKilled);
            throw new RuntimeException("ceshi rollback");
        });


    }
}
